package cn.donting.jme.edit.setting;

import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.File;
import java.io.IOException;

public abstract class AbsSetting {
    private static final ObjectMapper objectMapper = new ObjectMapper();
    private File file;
    public final void save() throws IOException {
        if (!file.exists()) {
            file.createNewFile();
        }
        objectMapper.writeValue(file, this);
    }
    public static <T extends AbsSetting> T read(File file, Class<T> tClass) throws Exception {
        AbsSetting setting;
        if(!file.exists()){
            file.createNewFile();
            setting = tClass.newInstance();
        }else{
            setting=objectMapper.readValue(file, tClass);
        }
       setting.file=file;
        return (T)setting;
    }
}
